package tr.helper.controller.server;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.net.URL;
import java.util.Date;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import org.apache.commons.lang3.StringUtils;
import tr.helper.CommonUtil;
import tr.helper.controller.SecurityController;
import tr.helper.controller.home.HomeMain;
import tr.helper.repository.dao.ServerDao;
import tr.helper.repository.table.ServerInfo;

public class ServerAddController extends SecurityController {

    @FXML
    private TextField name;
    @FXML
    private TextField host;
    @FXML
    private TextField port;
    @FXML
    private TextField username;
    @FXML
    private TextField password;
    @FXML
    private TextField sourceJar;
    @FXML
    private TextField targetJar;
    @FXML
    private TextField serverAppRoot;
    @FXML
    private TextField appPort;
    @FXML
    private Button btnSave;
    @FXML
    private TextField startExt;
    @FXML
    private ComboBox<String> type;
    @FXML
    private Label id;
    protected ServerDao serverDao = new ServerDao();

    @Override
    public void init(URL location, ResourceBundle resources) {
        id.setText(ServerListController.data);
        if (StringUtils.isNotBlank(ServerListController.data)) {
            btnSave.setDisable(false);
            final ServerInfo cond = new ServerInfo();
            cond.setId(Long.valueOf(ServerListController.data));
            ServerInfo info = serverDao.select(cond);
            if (null != info) {
                name.setText(info.getName());
                host.setText(info.getHost());
                port.setText(CommonUtil.asString(info.getPort()));
                username.setText(info.getUsername());
                password.setText(info.getPassword());
                sourceJar.setText(info.getSourceJar());
                targetJar.setText(info.getTargetJar());
                serverAppRoot.setText(info.getServerAppRoot());
                appPort.setText(CommonUtil.asString(info.getAppPort()));
                type.setValue(info.getType());
                startExt.setText(info.getStartExt());
            }
        } else {
            btnSave.setDisable(true);
        }
    }

    @FXML
    public void doAdd() {
        if (StringUtils.isBlank(username.getText())) {
            CommonUtil.alert(HomeMain.getInstance(), "用户名不能为空");
            return;
        }
        if (StringUtils.isBlank(password.getText())) {
            CommonUtil.alert(HomeMain.getInstance(), "密码不能为空");
            return;
        }
        if (StringUtils.isBlank(host.getText())) {
            CommonUtil.alert(HomeMain.getInstance(), "host不能为空");
            return;
        }
        if (StringUtils.isBlank(sourceJar.getText())) {
            CommonUtil.alert(HomeMain.getInstance(), "sourceJar不能为空");
            return;
        }
        if (StringUtils.isBlank(targetJar.getText())) {
            CommonUtil.alert(HomeMain.getInstance(), "targetJar不能为空");
            return;
        }
        if (type.getSelectionModel().getSelectedIndex() == 0) {
            if (StringUtils.isBlank(appPort.getText())) {
                CommonUtil.alert(HomeMain.getInstance(), "应用端口不能为空");
                return;
            }
        }

        if (StringUtils.isBlank(serverAppRoot.getText())) {
            CommonUtil.alert(HomeMain.getInstance(), "serverAppRoot不能为空");
            return;
        }
        String selectedType = type.getSelectionModel().getSelectedItem();
        ServerInfo serverInfo = ServerInfo.builder()
            .created(new Date())
            .name(name.getText())
            .host(host.getText())
            .port(Integer.parseInt(port.getText()))
            .username(username.getText())
            .password(password.getText())
            .sourceJar(sourceJar.getText())
            .targetJar(targetJar.getText())
            .serverAppRoot(serverAppRoot.getText())
            .startExt(startExt.getText())
            .type(selectedType)
            .build();
        if (StringUtils.isNotBlank(appPort.getText())) {
            serverInfo.setAppPort(Integer.parseInt(appPort.getText()));
        }
        if (StringUtils.isBlank(id.getText())) {
            serverInfo.setId(CommonUtil.nextId());
            serverDao.insertSelective(serverInfo);
        } else {
            serverDao.updateByPrimaryKey(serverInfo, Long.valueOf(id.getText()));
        }
        ServerListController.data = StringUtils.EMPTY;
        if (StringUtils.isNotBlank(id.getText())) {

        }
        CommonUtil.alert(null, "保存成功");
    }

    @FXML
    public void doClear() {
        name.clear();
        host.clear();
        port.clear();
        username.clear();
        password.clear();
        sourceJar.clear();
        targetJar.clear();
        serverAppRoot.clear();
        startExt.clear();
    }

    @FXML
    public void doConnection() {

        JSch jsch = new JSch();
        Session session;
        try {
            jsch.setKnownHosts(serverAppRoot.getText());
            // 创建SSH会话
            session = jsch.getSession(username.getText(), host.getText(), Integer.parseInt(port.getText()));
            session.setPassword(password.getText());
            // 忽略HostKey检查
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();
            CommonUtil.alert(null, "连接成功");
            btnSave.setDisable(false);
            session.disconnect();
        } catch (Exception e) {
            CommonUtil.alert(null, "连接失败:" + e.getMessage());
        }
    }

}
